﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Linq;
using Codeplex.ODataTFS.Model.Entities;
using Codeplex.ODataTFS.Model.Serialization;
using Microsoft.Data.Services.Toolkit.QueryModel;

#endregion

namespace Codeplex.ODataTFS.Model.Repositories
{
	public class BuildDefinitionRepository
	{
		private readonly TFSBuildDefinitionProxy _proxy;

		public BuildDefinitionRepository(TFSBuildDefinitionProxy proxy)
		{
			_proxy = proxy;
		}

		[RepositoryBehavior(HandlesFilter = true)]
		public IEnumerable<BuildDefinition> GetBuildDefinitionsByProject(ODataSelectManyQueryOperation operation)
		{
			if (operation == null)
			{
				throw new ArgumentNullException("operation");
			}

			return _proxy.GetBuildDefinitionsByProject(operation.Key);
		}

		public BuildDefinition GetOne(string definition, string project)
		{
			return _proxy.GetBuildDefinitionsByProject(project).SingleOrDefault(t => t.Definition.Equals(definition, StringComparison.OrdinalIgnoreCase));
		}

		public IEnumerable<BuildDefinition> GetAll()
		{
			throw new DataServiceException(501, "Not Implemented", "The 'Build Definitions' collection cannot be enumerated as a root collection. It should depend on a Project. (e.g. /Projects('12345')/Changes)", "en-US", null);
		}
	}
}
